package com.huanbao.nongwu.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.huanbao.common.api.CommonResult;
import com.huanbao.nongwu.pojo.beans.DeviceDoc;
import com.huanbao.nongwu.service.DeviceDocServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/* ━━━━━━佛祖保佑━━━━━━
 *                  ,;,,;
 *                ,;;'(    社
 *      __      ,;;' ' \   会
 *   /'  '\'~~'~' \ /'\.)  主
 * ,;(      )    /  |.     义
 *,;' \    /-.,,(   ) \    码
 *     ) /       ) / )|    农
 *     ||        ||  \)
 *     (_\       (_\
 * ━━━━━━永无BUG━━━━━━
 * @author ：zjq
 * @date ：2021/3/25 10:35
 * @description: TODO
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 */
@RestController
@RequestMapping("device")
public class DeviceDocController {


    @Autowired
    private DeviceDocServiceImpl iDeviceDocService;


    /**
     * 查询 设备档 --根据 站点 id
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("/getdevicedoc")
    public CommonResult getDeviceDoc(@RequestBody Map<String, Object> params) {
        PageInfo<DeviceDoc> deviceDocPageInfo = iDeviceDocService.selectByPaingAndCondition(params);
        return CommonResult.success(deviceDocPageInfo);

    }

    /**
     * 添加 设备
     *
     * @param deviceDocs
     * @param response
     * @param request
     * @throws IOException
     */
    @RequestMapping("/adddevicedoc")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult addDeviceDoc(@RequestBody List<DeviceDoc> deviceDocs) {
        if (deviceDocs.size() != 0) {
            String sewageid = deviceDocs.get(0).getSewageid().toString();
            iDeviceDocService.deleteBySewageid(Integer.valueOf(sewageid));
        }

        for (int i = 0; i < deviceDocs.size(); i++) {
            iDeviceDocService.insertSelective(deviceDocs.get(i));
        }

        return CommonResult.success();
    }


}
